<?php

/**
 * This is the model class for table "zy_agent_person".
 *
 * The followings are the available columns in table 'zy_agent_person':
 * @property integer $agent_person_id
 * @property integer $agent_number
 * @property integer $company_number
 * @property string $name
 * @property string $telephone
 * @property string $email
 * @property integer $user_id
 */
class AgentPerson extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AgentPerson the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_agent_person';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('agent_number, company_number, user_id', 'numerical', 'integerOnly'=>true),
			array('name, telephone, email', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('agent_person_id, agent_number,company_number,name, telephone, email, user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'agent_person_id' => 'Agent Person',
			'agent_number' => Yii::t('base','Agent Number'),
			'company_number' =>Yii::t('base','Company Number'),
			'name' => Yii::t('base','User Name'),
			'telephone' =>  Yii::t('base','telephone'),
			'email' =>Yii::t('base','email'),
			'user_id' => Yii::t('base','User Name'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('agent_person_id',$this->agent_person_id);
		$criteria->compare('agent_number',$this->agent_number);
		$criteria->compare('company_number',$this->company_number);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('telephone',$this->telephone,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('user_id',$this->user_id);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function judge($id){
		$rs=AgentPerson::model()->find('user_id='.$id);
		return $rs;
	}
	//custom下拉列表选择agent number
	public function getAgentNumber()
	{
		$rs = AgentPerson::model()->findAll();
		return CHtml::ListData($rs,'agent_person_id','agent_number');
	}
	public function getAgentPersonNumber($company_number){
		$sql="select * from zy_agent_person where company_number =".$company_number;
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql);
		$results = $command->queryAll();
		for($i=0;$i<count($results);$i++){
			$result[$i]=$results[$i]['agent_number'];
		}
		if($result==""){
			$agent_number=$company_number."01";
		}else{
			$num=count($result)-1;
			$agent_number=$result[$num]+1;
		}
		return $agent_number;
	}
	//创建公司总代理的时候 同时创建总经理
	public function createAgentPerson($user_id,$telephone,$email,$name,$company_number){
		$agentperson = new AgentPerson;
		$agentperson->user_id=$user_id;
		$agentperson->telephone=$telephone;
		$agentperson->email=$email;
		$agentperson->name=$name;
		$agentperson->company_number=$company_number;
		$agentperson->agent_number=self::getAgentPersonNumber($company_number);
		$agentperson->save();
	}
}